<template>
  <div class="mod-config">
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="dataForm.title" placeholder="请输入公告标题" clearable @clear="getDataList(1)"></el-input>
      </el-form-item>
      <el-form-item>
        <el-radio-group v-model="dataForm.isShow" @change="getDataList(1)">
          <el-radio-button label>全部</el-radio-button>
          <el-radio-button :label="0">未发布</el-radio-button>
          <el-radio-button :label="1">已发布</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button
          v-if="isAuth('notice:notice:save')"
          type="primary"
          @refreshDataList="getDataList"
          @click="addOrUpdateHandle()"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border style="width: 100%;" v-loading="dataListLoading">
      <el-table-column align="center" type="index" header-align="center" min-width="150" label="ID"></el-table-column>
      <el-table-column
        align="center"
        prop="title"
        header-align="center"
        min-width="150"
        label="公告标题"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="updateTime"
        header-align="center"
        min-width="150"
        label="更新时间"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="isShow"
        header-align="center"
        min-width="150"
        label="是否发布"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isShow === 1">已发布</el-tag>
          <el-tag type="info" v-if="scope.row.isShow === 0">未发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text"  @click="gotoDetail(scope.row)">管理</el-button>
          <el-button type="text" @click="delOrUpdateHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="dataForm.page"
      :page-sizes="[10, 20, 50]"
      :page-size="dataForm.limit"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!--    弹窗，新增/修改-->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
<!--    管理-->
    <notice-summary-detail ref="noticeSummaryDetail" />
  </div>
</template>

<script>
import AddOrUpdate from './notice-add-or-update'
import NoticeSummaryDetail from '../statistics/notice/notice-summary-detail'

export default {
  name: 'notice',
  data() {
    return {
      dataForm: {
        isShow: '',
        title: '',
        page: 1,
        limit: 10
      },
      dataList: [],
      dataListSelections: [],
      totalCount: 0,
      addOrUpdateVisible: false,
      editOrUpdateVisible: false,
      dataListLoading: false
    }
  },
  components: {
    AddOrUpdate,
    NoticeSummaryDetail
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList(page) {
      if (page) this.dataForm.page = page
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/news-admin/notice/list'),
        method: 'post',
        data: this.dataForm
      }).then(data => {
        if (data) {
          this.dataList = data.data.list
          this.totalCount = data.data.totalCount
        } else {
          this.dataList = []
          this.totalCount = 0
        }
        this.dataListLoading = false
      })
    },
    gotoDetail (notice) {
      this.$refs.noticeSummaryDetail.open(notice.id)
    },
    delOrUpdateHandle(id) {
      this.$confirm(`确定要进行删除操作吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/news-admin/notice/delete'),
          method: 'post',
          data: {
            ids: id || this.dataListSelections.map(item => item.id).join(',')
          }
        }).then(data => {
          if (data && data.code === 200) {
            this.$notify.success({
              title: '提示',
              message: '操作成功'
            })
            this.dataForm.page > 1 && this.dataList.length <= 1 && this.dataForm.page--
            this.getDataList()
          } else {
            this.$notify.warning({
              title: '提示',
              message: data.msg
            })
          }
        })
      })
    },
    sizeChangeHandle(val) {
      this.dataForm = Object.assign(this.dataForm, { page: 1, limit: val })
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.dataForm.page = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
  }
}
</script>

<style scoped>
</style>
